<?php

namespace app\admin\controller\product;

use app\common\controller\Backend;
// use app\admin\model\ProductType;

/**
 * 商品分类管理
 */
class Type extends Backend
{
  // 当前对象模型
  protected $model = null;

  public function _initialize()
  {
    parent::_initialize();
    $this->typeModel = model('Product.Type');

    $list = $this->typeModel->select();
    $catelist = [];
    foreach ($list as $item) {
      $catelist[$item['id']] = $item['name'];
    }
    $this->assign('catelist', $catelist);
  }

  // 查看
  /*  public function index()
  {
    if ($this->request->isAjax()) {

      list($where, $sort, $order, $offset, $limit) = $this->buildparams();

      $total = $this->model
        ->with($where)
        ->where($where)
        ->order($sort, $order)
        ->count();

      $list = $this->model
        ->with($where)
        ->where($where)
        ->order($sort, $order)
        ->limit($offset, $limit)
        ->select();

      $result = array("total" => $total, "rows" => $list);

      var_dump($result);exit;
      return json($result);
    }
    return $this->view->fetch();
  } */
  /*   public function index()
  {
    // 获取商品分类数据
    $productTypes = $this->model::select();

    // 将数据传递给视图
    $this->assign('productTypes', $productTypes);

    // 渲染视图模板
    return $this->fetch();
  } */
  public function index()
  {

    if ($this->request->isAjax()) {
      list($where, $sort, $order, $offset, $limit) = $this->buildparams();
      // halt($sort );

      $total = $this->typeModel
        ->where($where)
        ->order($sort, $order)
        ->count();

      $list = $this->typeModel
        ->where($where)
        ->order($sort, $order)
        ->limit($offset, $limit)
        ->select();

      $result = array("total" => $total, "rows" => $list);
      return json($result);
    }
    return $this->view->fetch();
  }


  // 添加
  public function add()
  {
    if ($this->request->isPOST()) {
      $params = $this->request->post("row/a", [], 'trim');
      $result = $this->typeModel->save($params);
      if (!$result) {
        $this->error($this->typeModel->getError());
      }

      $this->success();
    }
    return $this->view->fetch();
  }

  // 编辑
  public function edit($ids = null)
  {
    $row = $this->typeModel->find($ids);
    if (!$row) {
      $this->error('课程不存在');
    }
    if ($this->request->isPOST()) {
      $params = $this->request->post("row/a", [], 'trim');
      $params['id'] = $ids;
      $result = $this->typeModel->isUpdate()->save($params);
      if (!$result) {
        $this->error($this->typeModel->getError());
      }
      //编辑（当新图存在且与旧图不同时）
      if (!empty($params['thumbs']) && $params['thumbs'] != $row['thumbs']) {
        // 添加新图片，删除旧图片
        is_file(trim($row['thumbs'], '/')) && unlink(trim($row['thumbs'], '/'));
      }
      $this->success();
    }
    $this->assign('row', $row);
    return $this->view->fetch();
  }

  // 删除
  public function del($ids = null)
  {
    $row = $this->typeModel->all($ids);

    if (!$row) {
      $this->error('课程不存在');
    }

    $result = $this->typeModel->destroy($ids);

    if (!$result) {
      $this->error($this->typeModel->getError());
    }

    $this->success();
  }
}
